package com.itheima.factory;

import com.itheima.count.controller.ICountController;
import com.itheima.count.dao.ICountDao;
import com.itheima.count.service.ICountService;
import com.itheima.student.controller.IStudentController;
import com.itheima.student.dao.IStudentDao;
import com.itheima.student.service.IStudentService;
import com.itheima.user.controller.IUserController;
import com.itheima.user.controller.impl.UserControllerImpl;
import com.itheima.user.dao.IUserDao;
import com.itheima.user.service.IUserService;
import com.itheima.user.dao.impl.UserDaoImpl;
import com.itheima.user.service.impl.UserServiceImpl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import java.util.Set;

public class Factory {
    private static Properties properties=new Properties();
    static {
        try {
            //FileReader fis = new FileReader("system\\serious\\config.properties");
            FileInputStream fis = new FileInputStream("system\\serious\\config.properties");
            properties.load(fis);
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static IUserService getIUserService() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("IUserService")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                IUserService o1 = (IUserService)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static IUserDao getIUserDao() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("IUserDao")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                IUserDao o1 = (IUserDao)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static IUserController getIUserController() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("IUserController")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                IUserController o1 = (IUserController)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static IStudentController getIStudentController() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("IStudentController")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                IStudentController o1 = (IStudentController)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static IStudentService getIStudentService() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("IStudentService")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                IStudentService o1 = (IStudentService)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static IStudentDao getIStudentDao() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("IStudentDao")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                IStudentDao o1 = (IStudentDao)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static ICountController getICountController() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("ICountController")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                ICountController o1 = (ICountController)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

    public static ICountService getICountService() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("ICountService")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                ICountService o1 = (ICountService)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }
    public static ICountDao getICountDao() throws Exception {
        Set<Object> objects = properties.keySet();
        for (Object object : objects) {
            if(object.equals("ICountDao")){
                Object o = properties.get(object);
                String s = o.toString();
                Class<?> clazz = Class.forName(s);
                ICountDao o1 = (ICountDao)clazz.getConstructor().newInstance();
                return o1;
            }
        }
        return null;
    }

}
